$(function(){
	init();
	$("#btnDel").click(function(){
		if($("#tab").datagrid("getSelected") != null){
			var data = $("#tab").datagrid("getSelected");
			$.messager.confirm('Message', '删除该读者会附带删除该读者所有借阅书籍,确定删除?', function(r){  
                if (r){  
                	var params={
                			"readerBean.readerUsername":data.readerUsername
        			};
        			$.post("/library-mannager/reader/manage/delete.action",params,function(data){
        				$.messager.show({  
        	                title:'message',  
        	                msg:data.message.msg,  
        	                showType:'show'  
        	            });
        				$("#tab").datagrid({url:"/library-mannager/reader/manage/init.action"});
        			});
                }  
            });
		}else{
			$.messager.show({  
                title:'message',  
                msg:"请您选择需要删除的行",  
                showType:'show'  
            });
		}
	});
});

function init() {
	$("#tab").datagrid({
		view: detailview,
		detailFormatter:function(index,row){
			return detail(row);
		} ,
		url:"/library-mannager/reader/manage/init.action",
		singleSelect : true,
		fitColumns : true,
		pagination : true,
		fit : true,
		rownumbers:true,
		pagination:true  
		});
	var p = $('#tab').datagrid('getPager');  
    $(p).pagination({  
        beforePageText: '第',//页数文本框前显示的汉字  
        afterPageText: '页    共 {pages} 页',  
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',  
    }); 
}
function detail(row){
	var _data = "<table width='100%'><tr><th>借阅者</th><th>书籍ID</th><th>数量</th><th>借书日期</th></tr>";
	$.ajax({
		async:false,
		type:"POST",
		url:"/library-mannager/brrowed/book/reader.action",
		data:{readerUsername:row.readerUsername},
		success:function(data){
			for(var i = 0 ; i < data.data.length ; i++){
				_data =_data + "<tr><td>" + data.data[i].readerUsername + "</td><td>" + data.data[i].bookID + "</td><td>"
				+data.data[i].borrowedBookNumber+"</td><td>" + data.data[i].borrowedDate + "</td></tr>";
			}
		}
	});
	_data += "</table>";
	return _data;
}
